<template>

  <div class="boxForm_right">
    <el-form v-show="indexForm == 1" ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <div class="title">
        <div class="title-we2" style="width: 100%; display: flex; justify-content: center; align-items: center; padding-top: 20px;">
          <div>AIchat</div>
          <div class="banben">
            企业版 V1.0
          </div>
        </div>
      </div>
      <el-form-item prop="username" style="margin: 30px 0">
        <el-input v-model="loginForm.username" auto-complete="off" type="text" placeholder="请输入您的账号">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="password" style="margin: 30px 0">
        <el-input show-password v-model="loginForm.password" auto-complete="off" type="password" placeholder="请输入您的密码" @keyup.enter.native="handleLogin">
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <!-- <el-form-item prop="captcha" v-if="captchaOnOff" style="margin: 30px 0">
        <el-input v-model="loginForm.captcha" placeholder="请输入验证码" style="width: 63%" @keyup.enter.native="handleLogin">
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img" />
        </div>
      </el-form-item> -->
      <div style="display: flex; justify-content: space-between; height: 20px">
        <el-checkbox v-model="loginForm.rememberMe" style="margin: 0px 0px 25px 0px; color: #9d9d9d">记住密码</el-checkbox>
        <el-link @click="forget" style="height: 18px" :underline="false" type="primary">忘记密码?</el-link>
      </div>
      <el-form-item style="width: 100%; margin-top: 35px">
        <el-button :loading="loading" size="medium" type="primary" style="width: 100%; background-color: #556dff; border: 0; height: 42px" @click.native.prevent="handleLogin">
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
    </el-form>
    <el-form v-show="indexForm == 2" style="height: 500px" ref="loginForm1" :model="loginForm1" :rules="loginRules1" class="login-form">
      <div class="title" style="height: 70px; margin-top: 10px">
        <div class="title-we2">重置密码</div>
        <div class="title-we1"></div>
      </div>
      <el-form-item prop="code" style="margin: 30px 0">
        <el-input v-model="loginForm1.code" type="text" @keyup.enter.native="handleLogin1" placeholder="请输入您的账号">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="identityCardNo" style="margin: 30px 0">
        <el-input v-model="loginForm1.identityCardNo" type="text" placeholder="请输入您的身份证号" @keyup.enter.native="handleLogin1">
          <svg-icon slot="prefix" icon-class="WorkingStandard" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="phoneNo" style="margin: 30px 0">
        <el-input v-model="loginForm1.phoneNo" type="text" @keyup.enter.native="handleLogin1" placeholder="请输入您的手机号">
          <svg-icon slot="prefix" icon-class="phone" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item style="margin-top: 55px">
        <div style="display: flex; justify-content: space-between">
          <el-button :loading="loading" size="medium" style="width: 48%; height: 42px" @click="indexForm = 1">
            <span>返 回</span>
          </el-button>
          <el-button :loading="loading" size="medium" type="primary" style="width: 48%; background-color: #556dff; border: 0; height: 42px" @click.native.prevent="handleLogin1">
            <span v-if="!loading">立即重置</span>
            <span v-else>登 录 中...</span>
          </el-button>
        </div>
        <div class="foter_title" style="margin-bottom: 15px; margin-top: 10px;">
          温馨提示：密码重置成功后,默认密码为<span style="color: #fe312d">
            Nd2024!$%</span>。若重置不了，请联系系统负责人处理
        </div>
      </el-form-item>
    </el-form>

    <el-form v-show="indexForm == 4" style="height: 500px" ref="resettingFrom" :model="resettingFrom" :rules="resettingRules" class="login-form">
      <div class="title" style="height: 70px; margin-top: 10px">
        <div class="title-we2">修改密码</div>
        <div class="title-we1"></div>
      </div>
      <el-form-item prop="newPassword" style="margin: 30px 0">
        <el-input show-password v-model="resettingFrom.newPassword" auto-complete="off" type="password" placeholder="请您输入新密码">
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="confirmPassword" style="margin: 30px 0">
        <el-input show-password v-model="resettingFrom.confirmPassword" auto-complete="off" type="password" placeholder="请您再次输入新密码">
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item style="margin-top: 65px">
        <div style="display: flex; justify-content: space-between">
          <el-button :loading="loading" size="medium" type="primary" style="width: 100%; background-color: #556dff; border: 0; height: 42px" @click.native.prevent="updateUserPwd">
            <span>确认</span>
          </el-button>
        </div>
        <div class="foter_title" style="margin-bottom: 15px; color: #fe312d">
          温馨提示：新密码长度不小于10个字符，必须同时包含大写字母（A-Z）、小写字母（a-z）、数字（0-9）、特殊字符及标点符号。
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getCodeImg, resetLogin, defaultPas, updateUserPwd } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
export default {
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  data() {
    return {
      // 重置密码参数
      resettingFrom: {},
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        loginDeviceType: 2,
        code: "",
        uuid: "",
      },
      loginForm1: {
        code: "",
        identityCardNo: "",
        phoneNo: "",
      },
      loginRules1: {
        code: [{ required: true, trigger: "blur", message: "请您输入工号" }],
        identityCardNo: [
          { required: true, trigger: "blur", message: "请您输入身份证号" },
        ],
        phoneNo: [
          { required: true, trigger: "change", message: "请您输入手机号" },
        ],
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
        ],
        captcha: [
          { required: true, trigger: "change", message: "请输入验证码" },
        ],
      },
      // 修改密码参数
      resettingRules: {
        oldPassword: [
          { required: true, trigger: "blur", message: "旧密码不能为空" },
        ],
        newPassword: [
          { required: true, trigger: "blur", message: "新密码不能为空" },
        ],
        confirmPassword: [
          { required: true, trigger: "blur", message: "确认密码不能为空" },
        ],
      },
      loading: false,
      // 验证码开关
      captchaOnOff: true,
      redirect: undefined,
      indexForm: 1,
      token: undefined,
    };
  },
  methods: {
    // 忘记密码
    forget() {
      this.indexForm = 2;
    },
    getCode() {
      getCodeImg().then((res) => {
        this.captchaOnOff =
          res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          this.codeUrl = "data:image/gif;base64," + res.result.img;
          this.loginForm.captchaKey = res.result.key;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        loginDeviceType: 2,
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleLogin1() {
      this.$refs.loginForm1.validate((valid) => {
        if (valid) {
          resetLogin(this.loginForm1).then((res) => {
            if (res.stateCode === 200) {
              this.indexForm = 1;
              this.$modal.msgSuccess("重置密码成功");
            } else {
              this.$modal.msgSuccess(res.message);
            }
          });
        }
      });
    },
    updateUserPwd() {
      this.$refs.resettingFrom.validate((valid) => {
        if (valid) {
          this.loading = true;
          let data = {
            newPassword: this.resettingFrom.newPassword,
            code: this.loginForm.username,
            oldPassword: this.loginForm.password,
          };
          updateUserPwd(data, this.token)
            .then((res) => {
              if (res.stateCode === 200) {
                this.indexForm = 1;
                this.loading = false;
                this.loginForm = {};
                this.getCode();
                this.resetForm("loginForm");
                this.$modal.msgSuccess("修改密码成功");
              } else {
                this.$modal.msgSuccess(res.message);
                this.loading = false;
              }
            })
            .catch(() => {
              this.loading = false;
            });
        }
      });
    },
    handleLogin() {
      this.$router.push({ path: "/" })
      return
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), {
              expires: 30,
            });
            Cookies.set("rememberMe", this.loginForm.rememberMe, {
              expires: 30,
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          this.loginForm.account = this.loginForm.username;
          this.$store
            .dispatch("Login", this.loginForm)
            .then((res) => {
              let data = {
                code: this.loginForm.username,
              };
              defaultPas(data)
                .then(({ result, stateCode }) => {
                  if (result) {
                    this.$router
                      .push({ path: this.redirect || "/" })
                      .catch(() => {});
                  } else {
                    this.token = res;
                    this.$store.dispatch("FedLogOut").then(() => {});
                    this.loading = false;
                    this.indexForm = 4;
                  }
                })
                .catch(() => {
                  this.loading = false;
                });
            })
            .catch(() => {
              this.loading = false;
              if (this.captchaOnOff) {
                this.getCode();
              }
            });
        }
      });
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.boxForm_right {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
}

.title {
  height: 30px;
  margin-bottom: 20px;
}
.title-we1 {
  width: 46px;
  height: 5px;
  background: #666666;
  margin: 30px 0;
}

.title-we2 {
  width: 260px;
  height: 23px;
  font-size: 21px;
  font-family: Microsoft YaHei;
  font-weight: bold;
  margin-bottom: 5px;
  color: #666666;
}
.foter_title {
  width: 100%;
  height: 14px;
  font-size: 12px;
  font-family: Microsoft YaHei;
  font-weight: 400;
  text-align: center;
  color: #8b8b8b;
  line-height: 20px;
}
.banben {
  background-color: #6ed760;
  border-radius: 5px;
  padding: 5px;
  font-size: 12px;
  margin-left: 10px;
  color: #fff;
  font-weight: 700;
}

.login-form {
  border-radius: 6px;
  overflow: hidden;
  width: 100%;
  margin: 0 0;
  padding: 25px 0px 5px 0px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .el-input__inner {
    height: 38px !important;
    position: relative;
  }
  .input-icon {
    position: absolute;
    top: -4px;
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-code {
  width: 35%;
  height: 34px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 34px;
  width: 100%;
}
.login-form .el-input input {
  height: 46px;
}
.login-form .input-icon {
  height: 45px;
  margin-left: 5px;
}
.el-form-item__error {
  line-height: 2.5;
}
</style>
